package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ComparendoNegRein;

public class ComparendoNegReinDAOImpl extends JdbcDaoSupport implements IComparendoNegReinDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByRadicadoQuery findByRadicadoQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM OPE_COMPARENDO_NEG_REIN_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ComparendoNegRein comparendoNegRein) {
		deleteOperation.execute( comparendoNegRein  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ComparendoNegRein findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ComparendoNegRein comparendoNegRein) {
		insertOperation.execute( comparendoNegRein );
	}

	public void update(ComparendoNegRein comparendoNegRein) {
		updateOperation.execute( comparendoNegRein );
	}

	public ComparendoNegRein findByCriteria(ComparendoNegRein  comparendoNegRein) {
		return findByCriteriaQuery.runQuery(comparendoNegRein);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByRadicado(ComparendoNegRein comparendoNegRein) {
		return findByRadicadoQuery.runQuery(comparendoNegRein);
	}

	public ComparendoNegRein findByActiveState(ComparendoNegRein  comparendoNegRein) {
		return findByActiveStateQuery.runQuery(comparendoNegRein);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    //	deleteOperation = new DeleteOperation(  getDataSource() );
    //	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    //	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    //	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByRadicadoQuery = new FindByRadicadoQuery( getDataSource() );
    //	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO OPE_COMPARENDO_NEG_REIN_T ("
				  + " ID, "
				  + " ID_COMPARENDO, "
				  + " CODIGO, "
				  + " DETALLE, "
				  + " COMPROMISO_PAGO, "
				  + " FECHA_COMPROMISO, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "				  
				  + ") VALUES ("
			  	  + " OPE_COMPARENDO_NEG_REIN_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			compile();
		}
		public void execute(ComparendoNegRein comparendoNegRein) {
			InsertOperation.this.update( new Object[]{				
				comparendoNegRein.getIdComparendo(),
				comparendoNegRein.getCodigo(),
				comparendoNegRein.getDetalle(),
				comparendoNegRein.getCompromisoPago(),
				comparendoNegRein.getFechaCompromiso(),
				comparendoNegRein.getEstado(),
				comparendoNegRein.getUsuarioCrea()						
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE OPE_COMPARENDO_NEG_REIN_T SET "					  
					  + " ID_COMPARENDO = ? ,"
					  + " CODIGO = ? ,"
					  + " DETALLE = ? ,"
					  + " COMPROMISO_PAGO = ? ,"
					  + " FECHA_COMPROMISO = ? ,"
					  + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ComparendoNegRein comparendoNegRein) {
			UpdateOperation.this.update( new Object[]{				
				comparendoNegRein.getIdComparendo(),
				comparendoNegRein.getCodigo(),
				comparendoNegRein.getDetalle(),
				comparendoNegRein.getCompromisoPago(),
				comparendoNegRein.getFechaCompromiso(),
				comparendoNegRein.getEstado(),				
				comparendoNegRein.getUsuarioModifica(),
				comparendoNegRein.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_COMPARENDO_NEG_REIN_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(ComparendoNegRein comparendoNegRein) {
			this.update( new Object[]{
				comparendoNegRein.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_COMPARENDO_NEG_REIN_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ComparendoNegRein comparendoNegRein = new ComparendoNegRein();
				comparendoNegRein.setId(new Long(rs.getLong("ID")));
				comparendoNegRein.setIdComparendo(new Long(rs.getLong("ID_COMPARENDO")));
				comparendoNegRein.setCodigo(rs.getString("CODIGO"));
				comparendoNegRein.setDetalle(rs.getString("DETALLE"));
				comparendoNegRein.setCompromisoPago(rs.getString("COMPROMISO_PAGO"));
				comparendoNegRein.setFechaCompromiso(rs.getDate("FECHA_COMPROMISO"));		
				comparendoNegRein.setEstado(rs.getString("ESTADO"));
				comparendoNegRein.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				comparendoNegRein.setFechaCrea(rs.getDate("FECHA_CREA"));		
				comparendoNegRein.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				comparendoNegRein.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
				
				if(comparendoNegRein.getCompromisoPago().equals("S"))
					comparendoNegRein.setCompromisoPagoChk(true);				

			return comparendoNegRein;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ComparendoNegRein runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (ComparendoNegRein) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_COMPARENDO = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND DETALLE = ? "
			  	  	  					//+ " AND COMPROMISO_PAGO = ? "
			  	  	  					//+ " AND FECHA_COMPROMISO = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public ComparendoNegRein runQuery(ComparendoNegRein comparendoNegRein) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//comparendoNegRein.getId(),
				//comparendoNegRein.getIdComparendo(),
				//comparendoNegRein.getCodigo(),
				//comparendoNegRein.getDetalle(),
				//comparendoNegRein.getCompromisoPago(),
				//comparendoNegRein.getFechaCompromiso(),
				//comparendoNegRein.getEstado(),
				//comparendoNegRein.getUsuarioCrea(),
				//comparendoNegRein.getFechaCrea(),
				//comparendoNegRein.getUsuarioModifica(),
				//comparendoNegRein.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (ComparendoNegRein) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- FindAllQuery ----------------------------------------    
    class FindByRadicadoQuery extends BaseQueryOperation {
		private static final String sql = "SELECT NEG.* FROM OPE_COMPARENDO_NEG_REIN_T NEG INNER JOIN "
										+ " OPE_COMPARENDO_T COMP ON NEG.ID_COMPARENDO=COMP.ID " 
										+ " WHERE RADICADO=? OR '%'=? ";
    	public FindByRadicadoQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ComparendoNegRein comparendoNegRein) {
			List result =  execute( new Object[]{
					comparendoNegRein.getRadicado(),
					comparendoNegRein.getRadicado()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ComparendoNegRein runQuery(ComparendoNegRein comparendoNegRein) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ComparendoNegRein) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
